﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TimeManagementSystem
{
    public partial class Admin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //var mpLabel = (Label)Master.FindControl("userLogin");
            if (__EVENTTARGETCUR.Value == "editUser")
            {
                EditUsers(__EVENTARGUMENTCUR.Value);
            }
        }

        private void EditUsers(string editStr)
        {
            try
            {
                var elements = editStr.Split('~');
                if (elements[0].Length != 0)
                {
                    var usersToRemove = elements[0].Split(',');
                    for (var i = 0; i < usersToRemove.Length - 1; i++)
                    {
                        Global.inputValid.RemoveUser(Convert.ToInt32(usersToRemove[i]), Master.GetCreatorUser().Login);
                    }
                }
                var userInfo = elements[1].Split('$');
                for (var i = 0; i < userInfo.Length - 1; i++)
                {
                    var detailedInfo = userInfo[i].Split('+');
                    Global.inputValid.EditUser(Convert.ToInt32(detailedInfo[0]), detailedInfo[1],
                        detailedInfo[2], detailedInfo[3], detailedInfo[4], detailedInfo[5],
                        detailedInfo[6], Master.GetCreatorUser().Login);
                }
            }
            catch (Exception ex)
            {
                this.Master.ShowException(ex.Message);
            }
            finally
            {
                __EVENTTARGETCUR.Value = "";
                __EVENTARGUMENTCUR.Value = "";
            }
        }

        public List<User> GetSelectedUsers(List<User> allUsers)
        {
            if (userSelection.SelectedItem.Text == "All")
                return allUsers;
            var users = new List<User>();
            foreach (var user in allUsers)
            {
                if (user.Status == userSelection.SelectedItem.Text)
                    users.Add(user);
            }
            return users;
        }

        protected void addBtn_OnClick(object sender, EventArgs e)
        {
            if (passwordBox.Text == cpasswordBox.Text)
            {
                try
                {
                    if (loginBox.Text != "")
                    {
                        Global.inputValid.AddUser(nameBox.Text, snameBox.Text, patronymicBox.Text, emailBox.Text,
                            loginBox.Text, passwordBox.Text, roles.SelectedItem.Text, this.Master.user.Login);
                    }
                }
                catch (Exception ex)
                {
                    this.Master.ShowException(ex.Message);
                }
            }
        }
    }
}